

function say() {
    alert("211. 添加与搜索单词 - 数据结构设计");
}
say();

/**
 * Initialize your data structure here.
 */
var WordDictionary = function() {
    //3:42-42
    this.root = new TreeNode('/')
};

class TreeNode{
    constructor(val){
        this.val = val
        this.children = []
        this.isCharEnd = false
    }
}

/**
 * @param {string} word
 * @return {void}
 */
WordDictionary.prototype.addWord = function(word) {
    var root = this.root
    for(var i = 0; i < word.length; i++){
        var idx = word[i].charCodeAt(0) - 'a'.charCodeAt(0)
        if(!root.children[idx]) {
            root.children[idx] = new TreeNode(word[i])
        }
        root = root.children[idx]
    }
    root.isCharEnd = true
};

/**
 * @param {string} word
 * @return {boolean}
 */
WordDictionary.prototype.search = function(word) {
    return help(word, this.root)
};

function help(word, root) {
    for(var i = 0; i < word.length; i++){
        if(word[i] == '.'){
            var  arr = root.children
            for(var j = 0; j < 26; j++){
                if(arr[j] && help(word.substring(i + 1), arr[j])){
                    return true
                }
            }
            console.log('跟',root)
            return false
        }else {
            var idx = word[i].charCodeAt(0) - 'a'.charCodeAt(0)
            if(!root.children[idx]) {
                return false
            }
            root = root.children[idx]
        }
    }
    return root.isCharEnd
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * var obj = new WordDictionary()
 * obj.addWord(word)
 * var param_2 = obj.search(word)
 */